package rta.datos.conexion
{
	import flash.data.SQLConnection;
	import flash.data.SQLResult;
	import flash.data.SQLStatement;
	import flash.events.SQLErrorEvent;
	import flash.events.SQLEvent;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	
	import rta.datos.to.Perfil;
	import rta.datos.to.PerfilEstadistica;

	public class PerfilEstadisticaDB
	{
		private var conn:SQLConnection;
		private var statement:SQLStatement;
		private var _callback:Function;
		
		public function PerfilEstadisticaDB(conn:SQLConnection)
		{
			this.conn = conn;
			statement = new SQLStatement();
			statement.itemClass = PerfilEstadistica;
			statement.sqlConnection = conn;
			statement.addEventListener(SQLErrorEvent.ERROR, onSQLError);
		}
		
		public function set callback(value:Function):void
		{
			_callback = value;
		}
		
		private function onSQLError(event:SQLErrorEvent):void{
			trace(event.text);
		}
		
		//-------------------------------------------------
		
		
		//--OBTENER PERFILES CON DURACION DE SESIONES-----------
		public function obtenerPerfilesEstadistica():void{					
			statement.text = "SELECT pe.id_perfil id, " +
							       "pe.nombre nombres, " +
							       "pe.ap_paterno apellidoPaterno, " +
							       "pe.ap_materno apellidoMaterno, " +
							       "SUM(ss.duracion) duracionSesiones " +
							 "FROM perfil pe " +
							 "LEFT OUTER JOIN secuencia se ON se.id_perfil = pe.id_perfil " +
							 "LEFT OUTER JOIN sesion ss ON ss.id_secuencia = se.id_secuencia " +
							 "GROUP BY pe.id_perfil " +
							 "ORDER BY pe.ap_paterno ASC";
			statement.addEventListener(SQLEvent.RESULT, resultObtenerPerfilesEstadistica);
			statement.execute();
		}
		
		private function resultObtenerPerfilesEstadistica(event:SQLEvent):void{
			statement.removeEventListener(SQLEvent.RESULT, resultObtenerPerfilesEstadistica);
			var result:SQLResult = statement.getResult();
			if (result != null && this._callback != null) {  
				this._callback(new ArrayCollection(result.data));
			}
		}
		//-------------------------------------
	}
}